<?php
/**
 * 《我的产品》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/../mold/Mold.class.php');

class MoldMy extends Mold {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'客户报价' => array('cc'=>'customer.php?do=QuotePy.find', 'js'=>'1'),
			'产品信息' => array('cc'=>'mold.php?do=MoldMy.view_code', 'js'=>''),
			'我的产品' => array('cc'=>'mold.php?do=MoldMy.find', 'js'=>'1'),
			'产品资料' => array('cc'=>'mold.php?do=ReferenceMy.find', 'js'=>'1'),
				
			'货运需求' => array('cc'=>'express.php?do=RequirementMy.find', 'js'=>'1'),
		
			'注塑需求' => array('cc'=>'zs.php?do=RequirementMy.find', 'js'=>'1'),
			'注塑安排' => array('cc'=>'zs.php?do=PlanMy.find', 'js'=>''),	
		
			'报价信息' => array('cc'=>'mold.php?do=Quote.find', 'js'=>''),
			'报价明细' => array('cc'=>'mold.php?do=QuoteItem.find', 'js'=>''),			
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 year'));
		
		if ($this->_audit('25')) {
			$controller->assign('action_add', 'true');
		}
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '业务员',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'mold/mold/my/find');
		return true;
	}
	
	function _my() {		
		$emp = $_SESSION['id'];
		
		return sprintf("visible = 1 AND (emp = '%s' OR engineer = '%s' OR oper = '%s')", $emp, $emp, $emp);
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['color'] = $this->_status_color($row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'mold/mold/my/list');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'mold/mold/my/upload');
		return true;
	}
	
	function direct($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];		
		$customer = Common::id(Constant::tbl_customer, $id);
		
		$controller->assign_by_ref('customer', $customer);
		
		$controller->assign('t1', strtotime('28 day'));
				
		$controller->display($request, 'mold/mold/my/direct');
		return true;
	}
	
	function directPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		
		$c = Common::id(Constant::tbl_customer, $id);
		if (!$c) {
			$this->findPost($controller, $request);
			return true;
		}

		$code = $request['code'];
		$project = $this->_project($code);
		
		$category = $request['category'];
		$date = $request['date'];
		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(customer, project, code, category, date, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $c['id'], $project, $code, $category, $date, $content, $c['emp'], $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function manual($controller, $request) {
		$this->pretreat($controller);
				
		$controller->display($request, 'mold/mold/my/manual');
		return true;
	}
	
	function manualPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$code = $request['code'];
		$category = $request['category'];
		$date = $request['date'];
		$content = addslashes($request['content']);
		
		$c = $this->_customer($code);
		if (!$c) {
			$this->findPost($controller, $request);
			return true;
		}

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(customer, code, category, date, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $c['id'], $code, $category, $date, $content, $c['emp'], $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _customer($s) {
		list($code) = split("[\.]", strtoupper($s));
		
		$db = newdb(Constant::tbl_customer);
		$fmt = "SELECT * FROM %s WHERE code = '%s' AND emp = '%s'";
		$sql = sprintf($fmt, Constant::tbl_customer, $code, $_SESSION['id']);
		// debug($sql);
		$result = $db->query($sql);
		
		return $db->fetch_array($result);
	}
	
	function work($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		// $row['category'] = $this->category[$row['category']];
		$row['outer_t'] = $this->outer[$row['outer']];

		$row['emp_t'] = Name::emp($row['emp']);
		$row['engineer_t'] = Name::emp($row['engineer']);

		$row['status_t'] = $this->status[$row['status']];

		$row['work'] = json_encode($this->_mold_work($row['id']));
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/mold/my/work');
		return true;
	}
	
	function _mold_work($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/../mold/Info.class.php');
		$aa = new Info();
		
		return $aa->_list($id);
	}
	
	function workPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		
		$this->_update_mold_work($id, $request['work'], $emp);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_mold_work($id, $work, $emp) {		
		require_once(dirname(__FILE__) . '/../mold/Info.class.php');
		$aa = new Info();
		
		return $aa->_update($id, $work, $emp);
	}
	
	function z($controller, $request) {
		$s = "GM822.22-3";
		list($a, $b) = split("[\.]", $s);
		
		echo $b;

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
}
?>